﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SCRUM.Core.DAO;

namespace SCRUM.Core.BusinessObjects
{
    /// <summary>
    /// Business object for task.
    /// </summary>
    [Serializable()]
    public class Task : PriorityBusinessObject
    {
        /// <summary>
        /// Gets or sets the backlog item ID.
        /// </summary>
        /// <value>The bli ID.</value>
        public int BliID { get; set; }

 	    /// <summary>
        /// Initializes a new instance of the <see cref="Task"/> class.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <param name="name">The name.</param>
        /// <param name="desc">The description.</param>
        /// <param name="est">The estimation.</param>
        /// <param name="state">The task state.</param>
        public Task(int id, string name, string desc, double est, int stateID, double progress, int priority, DateTime change, int BliID)
            : base(id, name, desc, est, progress, priority, change, stateID)
        {
            this.stateType = State.StateTypes.Task;
            this.BliID = BliID;
        }

        /// <summary>
        /// Gets or sets the system ID.
        /// </summary>
        /// <value>The system ID.</value>
        public override string SystemID
        {
            get
            {
                return MakeSystemID("T",this.ID);
            }
        }

        /// <summary>
        /// Gets or sets the superior item's ID.
        /// </summary>
        /// <value>The superior item ID.</value>
        public override int SuperiorItemID
        {
            get { return this.BliID; }
            set { this.BliID = value; }
        }

        /// <summary>
        /// Gets or sets the backlog item.
        /// </summary>
        /// <value>The backlog item.</value>
        public BLI BacklogItem
        {
            get
            {
                // Lazy loading.
                if (this.backlogItem == null)
                {
                    this.backlogItem = DAOBli.GetBliByID(this.BliID);
                }
                return this.backlogItem;
            }
            set
            {
                this.backlogItem = value;
            }
        }

        /// <summary>
        /// Backlog item that contains this task.
        /// </summary>
        private BLI backlogItem;

        /// <summary>
        /// List of assigned user to the task.
        /// </summary>
        private IList<User> assignedUsers;

        /// <summary>
        /// Gets or sets users sassigned to task.
        /// </summary>
        /// <value>The Users assigned to task.</value>
        public IList<User> AssignedUsers
        {
            get
            {
                // Lazy loading
                if (this.assignedUsers == null)
                {
                    this.assignedUsers = DAOUser.GetUsers(this);
                }
                return this.assignedUsers;
            }
            set
            {
                this.assignedUsers = value;
            }
        }
        /// <summary>
        /// Returns string representation of Task
        /// </summary>
        /// <returns>Task name</returns>
        public override string ToString()
        {
            return String.Format("{0}", this.Name);
        }
                
    }
}
